﻿
using GCCGrpc.AuthCenter;

using Grpc.Core;
using Grpc.Net.Client;

namespace System.AuthCenter.Client
{
    /// <summary>
    /// 权限客户端管理器
    /// </summary>
    public class Manager
    {
        private static GrpcChannel? _channel = null;
        /// <summary>
        /// 初始化权限客户端
        /// </summary>
        /// <param name="host"></param>
        public static void Init(string host)
        {
            _channel = GrpcChannel.ForAddress(host);
        }
        /// <summary>
        /// 根据Token获取用户
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="currentEventID">当前追踪事件ID</param>
        /// <returns></returns>
        public static async Task<GetUserByTokenResponse> GetUserByToken(GetUserByTokenRequest request, string currentEventID)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            var res = await new GrpcServer.GrpcServerClient(_channel).GetUserByTokenAsync(request, metaData);
            return res;
        }
        /// <summary>
        /// 获取用户的所有登陆信息
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="currentEventID">当前追踪事件ID</param>
        /// <returns></returns>
        public static async Task<GetUserLoginInfosResponse> GetUserLoginInfos(GetUserLoginInfosRequest request, string currentEventID)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            return await new GrpcServer.GrpcServerClient(_channel).GetUserLoginInfosAsync(request, metaData);
        }


        /// <summary>
        /// 设置用户信息,如果不存在则新增
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="currentEventID">当前追踪事件ID</param>
        /// <returns></returns>
        public static async Task<SetUserResponse> SetUser(SetUserRequest request, string currentEventID)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            return await new GrpcServer.GrpcServerClient(_channel).SetUserAsync(request, metaData);
        }

        /// <summary>
        /// 获取用户的所有角色
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="currentEventID">当前追踪事件ID</param>
        /// <returns></returns>
        public static async Task<GetUserRolesResponse> GetUserRoles(GetUserRolesRequest request, string currentEventID)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            return await new GrpcServer.GrpcServerClient(_channel).GetUserRolesAsync(request, metaData);
        }
        /// <summary>
        /// 设置用户的角色
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="currentEventID">当前追踪事件ID</param>
        /// <returns></returns>
        public static async Task<SetUserRolesResponse> SetUserRoles(SetUserRolesRequest request, string currentEventID)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            return await new GrpcServer.GrpcServerClient(_channel).SetUserRolesAsync(request, metaData);
        }
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="currentEventID">当前追踪事件ID</param>
        /// <returns></returns>
        public static async Task<LoginResponse> Login(LoginRequest request, string currentEventID)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            return await new GrpcServer.GrpcServerClient(_channel).LoginAsync(request, metaData);
        }


        /// <summary>
        /// 获取用户的所有可用区域
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="currentEventID">当前追踪事件ID</param>
        /// <returns></returns>
        public static async Task<GetUserScopesResponse> GetUserScopes(GetUserScopesRequest request, string currentEventID)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            return await new GrpcServer.GrpcServerClient(_channel).GetUserScopesAsync(request, metaData);
        }
        /// <summary>
        /// 设置用户的可用区域
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="currentEventID">当前追踪事件ID</param>
        /// <returns></returns>
        public static async Task<SetUserScopesResponse> SetUserScopes(SetUserScopesRequest request, string currentEventID)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            return await new GrpcServer.GrpcServerClient(_channel).SetUserScopesAsync(request, metaData);
        }
        /// <summary>
        /// 注销用户
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="currentEventID">当前追踪事件ID</param>
        /// <returns></returns>
        public static async Task<LogoutResponse> Logout(LogoutRequest request, string currentEventID)
        {
            var metaData = new Metadata();
            if (!string.IsNullOrEmpty(currentEventID))
            {
                metaData.Add("GCCEventID", currentEventID);
            }
            return await new GrpcServer.GrpcServerClient(_channel).LogoutAsync(request, metaData);
        }

    }
}